package com.huangjue.backend.controller;

import com.huangjue.backend.utils.EncryptionUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 加密相关控制器
 * 提供RSA公钥获取等接口
 */
@RestController
@RequestMapping("/api/encryption")
@CrossOrigin(origins = "*")
public class EncryptionController {
    
    /**
     * 获取RSA公钥
     * 前端调用此接口获取公钥用于加密AES密钥
     * 
     * @return 包含公钥的响应
     */
    @GetMapping("/public-key")
    public ResponseEntity<Map<String, Object>> getPublicKey() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String publicKey = EncryptionUtil.getPublicKey();
            
            System.out.println("返回的公钥:");
            System.out.println(publicKey);
            
            if (publicKey != null && !publicKey.isEmpty()) {
                response.put("success", true);
                response.put("message", "获取公钥成功");
                response.put("data", publicKey);
                response.put("keyLength", publicKey.length());
                return ResponseEntity.ok(response);
            } else {
                response.put("success", false);
                response.put("message", "公钥未生成");
                return ResponseEntity.internalServerError().body(response);
            }
        } catch (Exception e) {
            System.err.println("获取公钥失败: " + e.getMessage());
            e.printStackTrace();
            response.put("success", false);
            response.put("message", "获取公钥失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 测试加密功能
     * 用于验证加密解密功能是否正常工作
     * 
     * @param testData 测试数据
     * @return 加密后的测试数据
     */
    @PostMapping("/test")
    public ResponseEntity<Map<String, Object>> testEncryption(@RequestBody Map<String, Object> testData) {
        Map<String, Object> response = new HashMap<>();
        
        try {
            String data = testData.get("data").toString();
            
            // 实际测试加密解密过程
            // 1. 生成AES密钥
            String aesKey = EncryptionUtil.generateAESKey();
            System.out.println("生成的AES密钥: " + aesKey);
            
            // 2. 使用AES加密数据
            String encryptedData = EncryptionUtil.encryptWithAES(data, aesKey);
            System.out.println("AES加密结果: " + encryptedData);
            
            // 3. 使用RSA加密AES密钥
            String encryptedAESKey = EncryptionUtil.encryptWithRSA(aesKey, EncryptionUtil.getPublicKey());
            System.out.println("RSA加密AES密钥结果: " + encryptedAESKey);
            
            // 4. 解密测试
            String decryptedAESKey = EncryptionUtil.decryptWithRSA(encryptedAESKey);
            System.out.println("解密后的AES密钥: " + decryptedAESKey);
            
            String decryptedData = EncryptionUtil.decryptWithAES(encryptedData, decryptedAESKey);
            System.out.println("解密后的数据: " + decryptedData);
            
            // 验证结果
            boolean success = data.equals(decryptedData);
            
            response.put("success", success);
            response.put("message", success ? "加密解密测试成功" : "加密解密测试失败");
            response.put("originalData", data);
            response.put("decryptedData", decryptedData);
            response.put("aesKey", aesKey);
            response.put("encryptedData", encryptedData);
            response.put("encryptedAESKey", encryptedAESKey);
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("加密测试失败: " + e.getMessage());
            e.printStackTrace();
            response.put("success", false);
            response.put("message", "加密测试失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
    
    /**
     * 测试密钥对一致性
     * 验证公钥和私钥是否匹配
     * 
     * @return 测试结果
     */
    @GetMapping("/test-keypair")
    public ResponseEntity<Map<String, Object>> testKeyPair() {
        Map<String, Object> response = new HashMap<>();
        
        try {
            // 生成测试数据
            String testData = "Hello World Test";
            
            // 获取公钥
            String publicKey = EncryptionUtil.getPublicKey();
            
            // 使用公钥加密
            String encryptedData = EncryptionUtil.encryptWithRSA(testData, publicKey);
            
            // 使用私钥解密
            String decryptedData = EncryptionUtil.decryptWithRSA(encryptedData);
            
            // 验证结果
            boolean success = testData.equals(decryptedData);
            
            response.put("success", success);
            response.put("message", success ? "密钥对测试成功" : "密钥对测试失败");
            response.put("originalData", testData);
            response.put("decryptedData", decryptedData);
            response.put("publicKeyLength", publicKey.length());
            
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            System.err.println("密钥对测试失败: " + e.getMessage());
            e.printStackTrace();
            response.put("success", false);
            response.put("message", "密钥对测试失败: " + e.getMessage());
            return ResponseEntity.internalServerError().body(response);
        }
    }
} 